//
//  PHTimingViewController.m
//  PhnixHome
//
//  Created by Levy Xu on 2017/11/27.
//  Copyright © 2017年 Levy Xu. All rights reserved.
//

#import "PHTimingViewController.h"
#import "TimePicker.h"

@interface PHTimingViewController ()
@property(nonatomic, strong) PHDevice *model;
@end

@implementation PHTimingViewController

- (instancetype)initWithDeviceModel:(PHDevice *)model {
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        _model = model;
        
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    WS(ws);
    NSArray *array = @[TIMER1_ON, TIMER1_OFF, TIMER2_ON, TIMER2_OFF ];
    [_model request:array animated:NO finish:^(BOOL success) {
        [ws.tableView reloadData];
    }];
}

- (void)setupNavigationItems {
    
    self.title = [NSString stringWithFormat:NSLocalizedString(@"appointment_timing", nil), nil];
    self.navRightTitle = NSLocalizedString(@"nav_save", nil);
    
    [super setupNavigationItems];
}

-(void)rightAction {
    TimePicker *datePicker1 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    TimePicker *datePicker2 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    LOG(@"timing1 %02d:%02d, timing2 %02d:%02d",(int)datePicker1.hour, (int)datePicker1.minute, (int)datePicker2.hour, (int)datePicker2.minute);
    
    if (!datePicker1.disabled && !datePicker2.disabled && labs(datePicker2.onTime - datePicker1.onTime) < 240) {
        [KKAlert warningWithText:NSLocalizedString(@"tips_timing_invalid", nil)];
        return;
    }
    
    NSDictionary *dict = @{ TIMER1_ON: @([datePicker1 onTime]),
                            TIMER1_OFF: @([datePicker1 offTime]),
                            TIMER2_ON: @([datePicker2 onTime]),
                            TIMER2_OFF: @([datePicker2 offTime]) };
    WS(ws);
    [_model modify:dict animated:YES finish:^(BOOL success) {
        if (success) {
            [ws.model request:@[ TIMER1_ON, TIMER1_OFF, TIMER2_ON, TIMER2_OFF ] animated:YES finish:^(BOOL success) {
//                [ws.navigationController popViewControllerAnimated:YES];
            }];
        }
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if(section == 0)
        return 10.0f;
    else
        return 40.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    } else {
        UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        QMUILabel *tipsLabel = [[QMUILabel alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 30, 20)];
        tipsLabel.font = CodeFontMake(14);
        tipsLabel.textColor = UIColorGray4;
        tipsLabel.textAlignment = NSTextAlignmentCenter;
        tipsLabel.text = @"提前3小时热水，滞后1小时关机";
        [footer addSubview:tipsLabel];
        return footer;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (IS_35INCH_SCREEN) {
        return 200;
    } else if (IS_40INCH_SCREEN) {
        return 220;
    }
    return 260;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identi = @"cell";
    TimePicker *cell = [tableView dequeueReusableCellWithIdentifier:identi];
    if (!cell) {
        cell = [[TimePicker alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identi];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@%d",NSLocalizedString(@"appointment_timing", nil), (int)indexPath.section + 1];
    if (indexPath.section == 0) {
        NSInteger on1 = [_model intWithCode:TIMER1_ON];
        NSInteger off1 = [_model intWithCode:TIMER1_OFF];
        if (off1 - on1 == 240 || on1 - off1 == 1200) {
            cell.disabled = NO;
            cell.hour = (on1 / 60 + 3) % 24;
            cell.minute = on1 % 60;
        } else {
            cell.disabled = YES;
            cell.hour = 7;
            cell.minute = 30;
        }
    } else {
        NSInteger on2 = [_model intWithCode:TIMER2_ON];
        NSInteger off2 = [_model intWithCode:TIMER2_OFF];
        if (off2 - on2 == 240 || on2 - off2 == 1200) {
            cell.disabled = NO;
            cell.hour = (on2 / 60 + 3) % 24;
            cell.minute = on2 % 60;
        } else {
            cell.disabled = YES;
            cell.hour = 19;
            cell.minute = 30;
        }
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
